package com.feijia.aijingjingus;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.feijia.aijingjingus.util.PreferencesUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import retrofit2.Retrofit;

public class MailSignupActivity extends AppCompatActivity {
    String toMail;
    EditText et_mail;
    Button bt_getCode;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mail_signup_);
        et_mail=(EditText)findViewById(R.id.et_mail);
        bt_getCode=(Button)findViewById(R.id.bt_getCode);
    }

    public void onClickBack(View view) {
        Intent intent = new Intent("signUp_Activity");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }

    public void getCode(View view) {
        toMail=et_mail.getText().toString();
        // 判断邮箱格式是否正确
        if(isValidEmail(toMail)) {
            PreferencesUtils.putString(getApplicationContext(), Constants.MAIL, et_mail.getText().toString());
            Intent intent = new Intent("MailSignupCodeActivity");
            startActivity(intent);
        }else {
            Toast.makeText(this, getString(R.string.Mailbox_format_error), Toast.LENGTH_SHORT).show();
        }
    }



    /**
     * 验证是否为正确的邮箱号
     *
     * @param email
     * @return
     */
    public static boolean isValidEmail(String email) {
        // 1、\\w+表示@之前至少要输入一个匹配字母或数字或下划线 \\w 单词字符：[a-zA-Z_0-9]
        // 2、(\\w+\\.)表示域名. 如新浪邮箱域名是sina.com.cn
        // {1,3}表示可以出现一次或两次或者三次.
        String reg = "\\w+@(\\w+\\.){1,3}\\w+";
        Pattern pattern = Pattern.compile(reg);
        boolean flag = false;
        if (email != null) {
            Matcher matcher = pattern.matcher(email);
            flag = matcher.matches();
        }
        return flag;
    }

}
